<?php

// echo $current_dir;
//if ($handle = opendir('C:\Users\Gary\Desktop\a')) {
//    while (false !== ($fileName = readdir($handle))) {
//        $newName = str_replace("SKU#","",$fileName);
//        rename($fileName, $newName);
//    }
//    closedir($handle);
//}


//
//$fullpath = 'ab/a.jpg';
//$additional = '1';
//while (file_exists($fullpath)) {
//    $info = pathinfo($fullpath);
//    $fullpath = $info['dirname'] . '/'
//              . $info['filename'] . $additional
//              . '.' . $info['extension'];
//}
//echo 'a';
//$files = glob("ab/*.jpg");
// foreach ($files as $filename) {
//    $info = pathinfo($filename);
//    $fullpath = strtoupper('new/'.$info['filename']). '.' . $info['extension'];
//  // $fullpath = 'os/'.str_replace('imported-','',$info['filename']). '.' . $info['extension'];
//    echo $fullpath.'<br />';
//    rename($filename, $fullpath);
//  //  echo "$filename size " . filesize($filename) . "\n";
//  }
  
   if ( isset( $_FILES['userfile'] ) )
    {
      $csv_file = $_FILES['userfile']['tmp_name'];
    
      if ( ! is_file( $csv_file ) )
        exit('File not found.');
    
    
    
      if (($handle = fopen( $csv_file, "r")) !== FALSE)
    
      {
    
          while (($data = fgetcsv($handle, 1500, ",")) !== FALSE)
          {
                $array[]=$data;
              
          }
          fclose($handle);
      }
      
     // print_r($array);exit;
        $len =count($array);
        $files = glob("ab/*.jpg");
        $i=0;
      foreach ($files as $filename) {
        
            $name=str_replace(" / ","",$array[$i][3]);
          
               
            $info = pathinfo($filename);
            $fullpath = strtoupper('new/'.$name). '.' . $info['extension'];
            echo $i.$fullpath.'<br />';
            rename($filename, $fullpath);
           // echo "$filename size  " . filesize($filename) . "\n";
          
            $i=$i+1;
        }
        
    }     

?>

<!DOCTYPE html>
<html>
<head>
  <title>CSV to MySQL Via PHP  for fresh list</title>
</head>
<body>
  <form enctype="multipart/form-data" method="POST">
    <input name="userfile" type="file">
    <input type="submit" value="Upload">
  </form>
</body>
</html>
